package com.tolerate.managespringboot.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tolerate.managespringboot.common.Constants;
import com.tolerate.managespringboot.common.Result;
import com.tolerate.managespringboot.entity.Dict;
import com.tolerate.managespringboot.entity.Menu;
import com.tolerate.managespringboot.entity.Role;
import com.tolerate.managespringboot.service.DictService;
import com.tolerate.managespringboot.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.rmi.MarshalledObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private DictService dictService;


    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody Menu menu) {
        if (menu.getId() == null) {
            if (menu.getPid() == null) {
                menu.setPid(0);
            }
            menuService.save(menu);
        } else {
            menuService.updateById(menu);
        }
        return Result.success();
    }

    @GetMapping("/page")
    public Result page(Integer pageNum, Integer pageSize, String name, String description) {
        IPage<Menu> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 使用条件构造查询，避免当参数为空时执行不必要的like操作
        if (name != null && !name.trim().isEmpty()) {
            lambdaQueryWrapper.like(Menu::getName, name.trim());
        }
        if (description != null && !description.trim().isEmpty()) {
            lambdaQueryWrapper.like(Menu::getDescription, description.trim());
        }

        IPage<Menu> menuIPage = menuService.page(page, lambdaQueryWrapper);

        // 返回数据
        Map<String, Object> map = new HashMap<>();
        map.put("data", menuIPage.getRecords());
        map.put("total", menuIPage.getTotal());
        return Result.success(map);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        boolean b = menuService.removeById(id);
        return Result.success();
    }

    @PostMapping("/delBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        boolean b = menuService.removeByIds(ids);
        return Result.success();
    }

    @PostMapping("/getMenuTree")
    public Result getMenuTree() {
        return menuService.getMenuTree();
    }

    @PostMapping("/getIcons")
    public Result getIcons() {
        LambdaQueryWrapper<Dict> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dict::getType, Constants.DICT_TYPE_ICON);
        List<Dict> list = dictService.list();
        return Result.success(list);
    }

}
